﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using DTO;

namespace DAO
{
    public class LoaiPhongDAO : ConnectionDAO
    {
        public static long KtMaLoaiPhong(long maLoaiPhong)
        {
            OleDbConnection cN = null;
            long temp = 0;

            try
            {
                cN = Cnect();

                string cmString = @"SELECT MaLoaiPhong
                                    FROM LOAIPHONG
                                    WHERE MaLoaiPhong=@MLP";

                OleDbCommand cM = new OleDbCommand(cmString, cN);
                OleDbParameter thamSo = new OleDbParameter("@MLP", OleDbType.Integer);
                thamSo.Value = maLoaiPhong;

                cM.Parameters.Add(thamSo);

                OleDbDataReader rDer = cM.ExecuteReader();

                while (rDer.Read())
                {
                    if (!rDer.IsDBNull(0))
                        temp = rDer.GetInt32(0);
                    else
                        temp = 0;
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (cN != null && cN.State == System.Data.ConnectionState.Open)
                    cN.Close();
            }


            if (temp == 0)
            {
                return 0;
            }
            return temp;
        }


        public static List<LoaiPhongDTO> LayDSLP()
        {
            OleDbConnection cN = null;
            List<LoaiPhongDTO> dsLP = new List<LoaiPhongDTO>();

            try
            {
                cN = Cnect();

                string cmString = @"SELECT LP.MaLoaiPhong,LP.LoaiPhong,DG.MaDonGia
                                    FROM LOAIPHONG LP,DONGIA DG
                                    WHERE LP.MaDonGia=DG.MaDonGia
                                    ORDER BY LP.MaLoaiPhong asc";

                OleDbCommand cM = new OleDbCommand(cmString, cN);
                OleDbDataReader rDer = cM.ExecuteReader();

                while (rDer.Read())
                {
                    LoaiPhongDTO lP = new LoaiPhongDTO();

                    if (!rDer.IsDBNull(0))
                        lP.MaLoaiPhong = rDer.GetInt32(0);
                    if (!rDer.IsDBNull(1))
                        lP.TenPhong = rDer.GetString(1);
                    if (!rDer.IsDBNull(2))
                        lP.MaDonGia.MaDonGia = rDer.GetInt32(2);

                    dsLP.Add(lP);
                }
            }
            catch (Exception ex)
            {
                dsLP = new List<LoaiPhongDTO>();
            }
            finally
            {
                if (cN != null && cN.State == System.Data.ConnectionState.Open)
                    cN.Close();
            }

            return dsLP;
        }

    }
}
